//
//  FoundViewController.m
//  QuickReadComic
//
//  Created by 苗钧豪 on 16/8/26.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "FoundViewController.h"
#import "HeaderView.h"
//#import "JHMacro.h"
#import "SixTableViewCell.h"
#import "FortySixTableViewCell.h"
#import "TwentyTableViewCell.h"
#import "TwoTableViewCell.h"
#import "OffcialTableViewCell.h"
#import "FoundTableViewCell.h"
#import "FoundView.h"
#import "ZWYNetTool.h"
#import "AFNetworking.h"
#import "InfosModel.h"
#import "TopicsModel.h"
#import "UserModel.h"
#import "FoundTableViewCell.h"
#import "LPRefresh.h"
#import "ViewController.h"
#import "MJRefresh.h"
@interface FoundViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property(nonatomic,strong)NSArray *segmentArray;
@property(nonatomic,strong)UISegmentedControl *segment;
@property(nonatomic,strong)HeaderView *header;// 头视图实例
@property(nonatomic,strong)UITableView *tableView;// 推荐界面
@property(nonatomic,strong)UIScrollView *ScrollView;// 发现界面整体scrollview
@property(nonatomic,strong)UIScrollView *scrollview;// 分类界面分类列表
@property(nonatomic,strong)UIScrollView *scrollViews;// 分类洁面详情列表
@property(nonatomic,strong)NSArray *btnArray;// 分类数组
@property(nonatomic,strong)FoundView *foundview;
@property(nonatomic,strong)NSMutableArray *titleMutableArray;
@property(nonatomic,strong)UIButton *SBtn;
@property(nonatomic,strong)NSMutableArray *tagArray;
@end

@implementation FoundViewController
// 下拉刷新
- (void)viewDidAppear:(BOOL)animated {
    __weak FoundViewController *weakSelf = self;
    [self.tableView addRefreshWithBlock:^{
        [weakSelf NetworkRequests];
    }];

}
- (void)viewDidLoad {
    // 网络请求
    [self NetworkRequests];
    [super viewDidLoad];
    self.tagArray = [NSMutableArray array];
    self.titleMutableArray = [NSMutableArray array];
    // 导航栏不透明
    self.navigationController.navigationBar.translucent = NO;
    UIImage *image = [UIImage imageNamed:@"IMG_0427"];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[image imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] style:(UIBarButtonItemStylePlain) target:self action:@selector(seek)];
    self.navigationItem.rightBarButtonItem = item;
    // 两个界面通过ScrollView联系
    self.ScrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    // 偏移量
    _ScrollView.contentOffset = CGPointMake(0, 0);
    // 滚动范围
    _ScrollView.contentSize = CGSizeMake(self.view.bounds.size.width * 2, self.view.bounds.size.height);
    // 整页翻动
    _ScrollView.pagingEnabled = YES;
    // 是否回弹
    _ScrollView.bounces = NO;
    // 设置代理人
    _ScrollView.delegate = self;
    // 添加到父视图
    [self.view addSubview:self.ScrollView];
    
    [self setup];
    [self recommend];
    [self classify];
    
    
    
}
- (void)seek {
    ViewController *vc = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
//    [self.navigationController pushViewController:vc animated:YES];
    [self presentViewController:vc animated:YES completion:nil];
}
- (void)refreshStateChange:(UIRefreshControl *)control {
    [ZWYNetTool GET:@"http://api.kuaikanmanhua.com/v1/topic_new/discovery_list?" andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        NSLog(@"#####################%@",result);
        NSLog(@"=====================成功啦");
        self.model = [dataModel modelWithDic:result[@"data"]];
        [self.tableView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        NSLog(@"=====================出错啦");
        
    }];
}
// 标题栏视图
- (void)setup {
    // 设置分块控件
    self.segmentArray = [NSArray arrayWithObjects:@"推荐",@"分类", nil];
    self.segment = [[UISegmentedControl alloc]initWithItems:_segmentArray];
    self.segment.frame = CGRectMake(0, 0, 140, 30);
    // 倒圆角
    CALayer *layer = self.segment.layer;
    [layer setMasksToBounds:YES];
    [layer setCornerRadius:15];
    // 分块控件开始处于第一个
    self.segment.selectedSegmentIndex = 0;
    // 控件颜色
    self.segment.tintColor = [UIColor colorWithRed:0.953 green:0.651 blue:0.075 alpha:1.000];
    // 控件方法
    [self.segment addTarget:self action:@selector(segmentAction) forControlEvents:(UIControlEventValueChanged)];
    // 添加到导航栏
    self.navigationController.navigationBar.topItem.titleView = _segment;
}
//********************** 网络请求
- (void)NetworkRequests {
    [ZWYNetTool GET:@"http://api.kuaikanmanhua.com/v1/topic_new/discovery_list?" andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        NSLog(@"#####################%@",result);
        NSLog(@"=====================成功啦");
        self.model = [dataModel modelWithDic:result[@"data"]];
        [self.tableView reloadData];
        [self.tableView endRefreshingSuccess];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"=====================出错啦");
        [self.tableView endRefreshingFail];
    }];
}
//********************** 推荐界面
- (void)recommend {
    // 创建tableview
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 115) style:(UITableViewStyleGrouped)];
    // 指定代理人
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    // 注册cell
    [_tableView registerNib:[UINib nibWithNibName:@"SixTableViewCell" bundle:nil] forCellReuseIdentifier:@"6"];
    [_tableView registerNib:[UINib nibWithNibName:@"FortySixTableViewCell" bundle:nil] forCellReuseIdentifier:@"46"];
    [_tableView registerNib:[UINib nibWithNibName:@"TwentyTableViewCell" bundle:nil] forCellReuseIdentifier:@"20"];
    [_tableView registerNib:[UINib nibWithNibName:@"TwoTableViewCell" bundle:nil] forCellReuseIdentifier:@"2"];
    [_tableView registerNib:[UINib nibWithNibName:@"OffcialTableViewCell" bundle:nil] forCellReuseIdentifier:@"100"];
    [self.ScrollView addSubview:_tableView];
    // 头视图轮播图位置
    self.header = [[HeaderView alloc]initWithFrame:CGRectMake(0, -150, self.view.bounds.size.width, 150)];
    self.tableView.tableHeaderView = _header;
}
// 每个分区1个cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
// 14个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 14;
}
// 分区足高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
// tableViewCell大小
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 || indexPath.section == 5 || indexPath.section == 7 || indexPath.section == 9 || indexPath.section == 11 || indexPath.section == 13) {
        return 438;
    } else if (indexPath.section == 1 || indexPath.section == 4 || indexPath.section == 6 || indexPath.section == 10 || indexPath.section == 12) {
        return 200;
    } else if (indexPath.section == 3) {
        return 300;
    } else if (indexPath.section == 2) {
        return 325;
    } else {
        return 195;
    }
}
// 设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    InfosModel *infos = self.model.infos[indexPath.section + 1];
    if (indexPath.section == 0 || indexPath.section == 5 || indexPath.section == 7 || indexPath.section == 9 || indexPath.section == 11 || indexPath.section == 13) {
            SixTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"6" forIndexPath:indexPath];
            cell.Number = indexPath.section + 1;
            NSLog(@"*******************************%ld",cell.Number);
            cell.titleLabel.text = infos.title;
            return cell;
        } else if (indexPath.section == 1 || indexPath.section == 4 || indexPath.section == 6 || indexPath.section == 10 || indexPath.section == 12) {
            FortySixTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"46" forIndexPath:indexPath];
            cell.Number = indexPath.section + 1;
            cell.titleLabel.text = infos.title;
            return cell;
        } else if (indexPath.section == 3) {
            TwentyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"20" forIndexPath:indexPath];
            cell.Number = indexPath.section + 1;
            cell.titileLabel.text = infos.title;
            return cell;
        } else if (indexPath.section == 2) {
            TwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"2" forIndexPath:indexPath];
            cell.Number = indexPath.section + 1;
            cell.titleLabel.text = infos.title;
            return cell;
        } else {
            OffcialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"100" forIndexPath:indexPath];
            cell.Number = indexPath.section + 1;
            cell.titleLabel.text = infos.title;
            return cell;
        }
}
//********************** 分类界面
- (void)classify {
    // 数组初始化(假数据)
    self.btnArray = [NSArray arrayWithObjects:@"全部",@"恋爱",@"爆笑",@"奇幻",@"恐怖",@"耽美",@"剧情",@"成人",@"日常",@"治愈",@"百合",@"三次元", nil];
    // 创建分类scrollview
    self.scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, 30)];
    // 偏移量
    _scrollview.contentOffset = CGPointMake(0, 0);
    // 滚动范围
    _scrollview.contentSize = CGSizeMake(self.view.bounds.size.width / 5 * 12, 30);
    // 循环创建button
    for (int i = 0; i < 12; i ++) {
        _btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _btn.frame = CGRectMake((self.view.bounds.size.width) / 5 * i, 0, self.view.bounds.size.width / 5, 30);
        // 设置字体颜色、内容、大小
        [_btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_btn setTitle:[NSString stringWithFormat:@"%@",_btnArray[i]] forState:(UIControlStateNormal)];
        [_btn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        _btn.titleLabel.font = [UIFont fontWithName:@"Arial" size:13];
        [_tagArray addObject:_btn];
        if (i == 0) {
            _btn.selected = YES;
            self.SBtn = _btn;
            [_btn setTitleColor:[UIColor colorWithRed:0.953 green:0.651 blue:0.075 alpha:1.000] forState:(UIControlStateNormal)];
        }
        [_scrollview addSubview:_btn];
    }
    [self.ScrollView addSubview:_scrollview];
    // 创建详情scrollview
    self.scrollViews = [[UIScrollView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width, 30, self.view.bounds.size.width, self.view.bounds.size.height - 30)];
    // 偏移量
    _scrollViews.contentOffset = CGPointMake(0, 0);
    // 滚动范围
    _scrollViews.contentSize = CGSizeMake(self.view.bounds.size.width * 12, self.view.bounds.size.height - 30);
    // 整页翻动
    _scrollViews.pagingEnabled = YES;
    // 是否回弹
    _scrollViews.bounces = NO;
    _scrollViews.delegate = self;
    // 循环创建view
    for (int i = 0; i < 12; i ++) {
        self.foundview = [[FoundView alloc]initWithFrame:CGRectMake(i * self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        [_scrollViews addSubview:self.foundview];
    }
    [self.ScrollView addSubview:_scrollViews];
}
- (void)btnAction:(UIButton *)btn {
    if (btn.selected == YES) {
//        _scrollViews.contentOffset = CGPointMake(btn.frame.origin.x * 5, 0);
        [_scrollViews setContentOffset:CGPointMake(btn.frame.origin.x * 5, 0) animated:YES];
    } else {
        self.SBtn.selected = NO;
        [self.SBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        btn.selected = YES;
        self.SBtn = btn;
        if (btn.frame.origin.x != 0 && btn.frame.origin.x != btn.frame.size.width && btn.frame.origin.x != 10 * btn.frame.size.width && btn.frame.origin.x != 11 * btn.frame.size.width) {
//            _scrollview.contentOffset = CGPointMake(btn.frame.origin.x - (2 * btn.frame.size.width), 0);
            [_scrollview setContentOffset:CGPointMake(btn.frame.origin.x - (2 * btn.frame.size.width), 0) animated:YES];
        }
        [btn setTitleColor:[UIColor colorWithRed:0.953 green:0.651 blue:0.075 alpha:1.000] forState:(UIControlStateNormal)];
//        _scrollViews.contentOffset = CGPointMake(btn.frame.origin.x * 5, 0);
        [_scrollViews setContentOffset:CGPointMake(btn.frame.origin.x * 5, 0) animated:YES];
    }
}
- (void)segmentAction {
    if (self.segment.selectedSegmentIndex == 0) {
        // 控制滚动视图偏移量
//        self.ScrollView.contentOffset = CGPointMake(0, 0);
        [self.ScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    } else if (self.segment.selectedSegmentIndex == 1) {
//        self.ScrollView.contentOffset = CGPointMake(self.view.bounds.size.width, 0);
        [self.ScrollView setContentOffset:CGPointMake(self.view.bounds.size.width, 0) animated:YES];
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    scrollView = _scrollViews;
    NSInteger number = scrollView.contentOffset.x / self.view.bounds.size.width;
    if (number != 0 && number != 1 && number != 10 && number != 11) {
        _scrollview.contentOffset = CGPointMake(number * 75 - 150, 0);
    } 
    for (UIButton *button in _tagArray) {
        [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    }
    UIButton *btn = _tagArray[number];
    [btn setTitleColor:[UIColor colorWithRed:0.953 green:0.651 blue:0.075 alpha:1.000] forState:(UIControlStateNormal)];
    if (_ScrollView.contentOffset.x == 0) {
        _segment.selectedSegmentIndex = 0;
    } else if (_ScrollView.contentOffset.x != 0) {
        _segment.selectedSegmentIndex = 1;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
